﻿/// <reference path="../Framework/BrowserApi.js" />
/// <reference path="../Framework/Toolbar/Toolbar.css" />
/// <reference path="Content.js" />
/// <reference path="Global.js" />
/// <reference path="MouseAndKeyboardEvents.js" />
/// <reference path="Spreadsheet.js" />
/// <reference path="VirtualScrolling.js" />
/// <reference path="ToolbarItems.js" />

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
var SelectionProcessing = new function ()
{
    // Locate the start and end of the selected area.
    this.SelectedArea =
    {
        SelectedCell:
        {
            RowId: 1,
            ColumnId: 1
        },
        From:
        {
            RowId: 1,
            ColumnId: 1,
            X: 0,
            Y: 0
        },
        To:
        {
            RownId: 1,
            ColumnId: 1,
            X: 0,
            Y: 0
        }
    }

    this.SelectingCells = false

    // Mark the selected area.
    this.SelectArea = function ()
    {
        // Translate the mouse location to rows and columns.
        var selectedRowId = parseInt(this.SelectedArea.SelectedY / Spreadsheet.StandardRowHeight, 0)
        var selectedColumnId = parseInt((this.SelectedArea.SelectedX - Spreadsheet.StandardRowNumberWidth + Spreadsheet.StandardColumnWidth) / Spreadsheet.StandardColumnWidth, 0)

        var currentRowId = parseInt(this.SelectedArea.CurrentY / Spreadsheet.StandardRowHeight, 0)
        var currentColumnId = parseInt((this.SelectedArea.CurrentX - Spreadsheet.StandardRowNumberWidth + Spreadsheet.StandardColumnWidth) / Spreadsheet.StandardColumnWidth, 0)

        // Locate the beginning and end of the selected area.
        var startRow = selectedRowId
        var endRow = currentRowId
        if (startRow > endRow)
        {
            endRow = selectedRowId
            startRow = currentRowId
        }

        var startColumn = selectedColumnId
        var endColumn = currentColumnId
        if (startColumn > endColumn)
        {
            endColumn = selectedColumnId
            startColumn = currentColumnId
        }

        // Adjust the selected area with the column spans between the start row and end row.
        // Repeat the loop until all qualified elements are selected.
        for (var rowIndex = startRow; rowIndex <= endRow; rowIndex++)
        {
            var dataRow = SpreadsheetContent.Rows[rowIndex];
            if (dataRow)
            {
                for (var dataCellIndex in dataRow.Cells)
                {
                    var dataCell = dataRow.Cells[dataCellIndex]
                    var newColumnId = parseInt(dataCellIndex, 0)
                    var startOver = false;

                    if (startColumn > newColumnId && startColumn < newColumnId + dataCell.ColumnSpan)
                    {
                        startColumn = newColumnId
                        startOver = true;
                    }

                    if (endColumn >= newColumnId && endColumn < newColumnId + dataCell.ColumnSpan - 1)
                    {
                        endColumn = newColumnId + dataCell.ColumnSpan - 1
                        startOver = true;
                    }

                    if (startOver == true)
                    {
                        rowIndex = startRow - 1
                        break
                    }
                }
            }
        }

        // Highlight the selected columns and rows
        for (var rowIndex = 1; rowIndex <= SpreadsheetTable.rows.length - 1; rowIndex++)
        {
            if (rowIndex >= startRow && rowIndex <= endRow)
            {
                SpreadsheetTable.rows[rowIndex].cells[0].className = "RowNumberSelectedCellStyle"
            }
            else
            {
                SpreadsheetTable.rows[rowIndex].cells[0].className = "RowNumberCellStyle"
            }
        }
        for (var columnIndex = 1; columnIndex <= SpreadsheetTable.rows[0].cells.length - 1; columnIndex++)
        {
            if (columnIndex >= startColumn && columnIndex <= endColumn)
            {
                SpreadsheetTable.rows[0].cells[columnIndex].className = "ColumnHeaderSelectedCell"
            }
            else
            {
                SpreadsheetTable.rows[0].cells[columnIndex].className = "ColumnHeaderCell"
            }
        }

        // Highlight the selected area.
        CellHighlight.style.top = (startRow * Spreadsheet.StandardRowHeight) - 1 + "px"
        CellHighlight.style.left = (startColumn * Spreadsheet.StandardColumnWidth) + Spreadsheet.StandardRowNumberWidth - Spreadsheet.StandardColumnWidth - 1 + "px"
        CellHighlight.style.height = ((endRow - startRow + 1) * Spreadsheet.StandardRowHeight) + "px"
        CellHighlight.style.width = ((endColumn - startColumn + 1) * Spreadsheet.StandardColumnWidth) + "px"
    }


    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    this.MoveSelectedCellLeft = function ()
    {

    }

    this.MoveSelectedCellUp = function ()
    {
    }

    this.MoveSelectedCellRight = function ()
    {
    }

    this.MoveSelectedCellDown = function ()
    {
    }

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    this.ExpandSelectedAreaLeft = function ()
    {
    }

    this.ExpandSelectedAreaUp = function ()
    {
    }

    this.ExpandSelectedAreaRight = function ()
    {
    }

    this.ExpandSelectedAreaDown = function ()
    {
    }

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    this.AddColumnsAtEndOfDocument = function ()
    {
    }

    this.AddRowsAtEndOfDocument = function ()
    {
    }

    this.RemoveColumnsAtEndOfDocument = function ()
    {
    }

    this.RemoveRowsAtEndOfDocument = function ()
    {
    }
}